﻿Imports Odyssee.WebControles
Imports OpenDelib.ServiceEntreprises

Public Class CtrlSelectionEntreprises
    Inherits SelectionControle(Of Entreprise, EntreprisesClient, ParametresSuppressionEntreprise, CriteresRechercheEntreprise)

    Protected Overrides ReadOnly Property LibelleElement As String
        Get
            Return "entreprise"
        End Get
    End Property
    Protected Overrides ReadOnly Property LibelleFeminin As Boolean
        Get
            Return True
        End Get
    End Property
    Protected Overrides ReadOnly Property LibelleApostrophe As Boolean
        Get
            Return True
        End Get
    End Property
    Protected Overrides ReadOnly Property UrlFicheElement As String
        Get
            Return "~/Tiers/FicheEntreprise.aspx"
        End Get
    End Property
    Protected Overrides ReadOnly Property EntiteParametrageType As Integer
        Get
            Return 6
        End Get
    End Property
    Protected Overrides ReadOnly Property GridViewResultats As AcroGridView
        Get
            Return gvResultats
        End Get
    End Property
    Protected Overrides ReadOnly Property ComboType As DropDownList
        Get
            Return cmbType
        End Get
    End Property
    Protected Overrides ReadOnly Property BoutonRechercher As Button
        Get
            Return btnRechercher
        End Get
    End Property
    Protected Overrides ReadOnly Property DivRecherche As HtmlGenericControl
        Get
            Return divCriteres
        End Get
    End Property
    Protected Overrides ReadOnly Property DivGridView As HtmlGenericControl
        Get
            Return divResultats
        End Get
    End Property
    Protected Overrides ReadOnly Property FirstControlFocused As Control
        Get
            Return Me.txtEntreprise
        End Get
    End Property


    Private Sub gvResultats_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvResultats.RowDataBound
        Dim indexColonne As Integer = -1

        For i As Integer = 0 To Me.gvResultats.Columns.Count - 1
            Dim field As BoundField = TryCast(Me.gvResultats.Columns(i), BoundField)
            If field IsNot Nothing AndAlso field.DataField.ToLower = "codesiret" Then indexColonne = i : Exit For
        Next

        If indexColonne >= 0 AndAlso indexColonne <= Me.gvResultats.Columns.Count - 1 Then
            Dim entreprise As Entreprise = Tools.GetRowDataItem(Of Entreprise, List(Of Entreprise))(Me.gvResultats, e.Row)
            If entreprise Is Nothing Then Return
            If entreprise.CodeSIRET = 0 Then
                e.Row.Cells(indexColonne).Text = ""
            ElseIf entreprise.CodeSIRET.ToString.Length = 9 Then
                e.Row.Cells(indexColonne).Text = entreprise.CodeSIRET.ToString("### ### ###")
            End If
        End If
    End Sub


    Protected Overrides Function InitialiserParametresSuppression(cle As Object) As ParametresSuppressionEntreprise
        If String.IsNullOrEmpty(CStr(cle)) Then Return Nothing

        Dim params As New ParametresSuppressionEntreprise
        params.Authentication = New AuthenticationParameters
        params.Authentication.Token = Tools.GetInstance.getToken
        params.Cle = CStr(cle)
        params.SuppressionTotale = True

        Return params
    End Function
    Protected Overrides Sub Supprimer(svc As EntreprisesClient, params As ParametresSuppressionEntreprise, ByRef ok As Boolean, ByRef messageErreur As String)
        Dim results As OperationResult
        results = svc.Suppression(params)
        ok = results.OK
        messageErreur = results.ErrorMessage
    End Sub

    Protected Overrides Function InitialiserCriteresRecherche() As CriteresRechercheEntreprise
        Dim params As New CriteresRechercheEntreprise
        params.Authentication = New AuthenticationParameters
        params.Authentication.Token = Tools.GetInstance.getToken
        params.CleCommencantPar = Me.txtEntreprise.Text
        params.Type = Me.cmbType.SelectedValue
        Return params
    End Function
    Protected Overrides Function Rechercher(svc As EntreprisesClient, criteres As CriteresRechercheEntreprise, ByRef ok As Boolean, ByRef messageErreur As String) As List(Of Entreprise)
        Dim results As ResultatRechercheEntreprise = svc.Recherche(criteres)
        If results Is Nothing Then Return Nothing
        ok = results.OK
        messageErreur = results.ErrorMessage
        Return results.Entreprises
    End Function

End Class